import {PollingFunc} from './utils'
export const baseData = {
    loginPage: false, // 判断是否在登录页面
    mainIframeTop:null,
    mainIframe:null, // 
    mainIframeTopDocument: document, // 在mainiframe上层iframe的document对象
    mainIframeDocument:null, // 在mainiframe里头的document对象
    forwardFrame:null, // 在mainiframe里头的document对象
}


export function onReady () {
    baseData.document = document
    return new Promise ((resolve,reject) => {
        baseData.loginPage = location.href.includes('login/main/index.html')
        // 如果是在jira页面
        if (window.top.location.href.includes('nccjira')) {
            setTimeout(() => {
                resolve()
            }, 500);
            return 
        }        
        // 唯一定时器，等待页面渲染完成
        setTimeout(() => {  
            // 轮询,直到需要的baseData下的变量赋值好必要的值再resolve
            PollingFunc(() => {
                let forwardFrame = document.querySelector('#forwardFrame')
                if (forwardFrame) {
                    baseData.forwardFrame = forwardFrame
                    baseData.mainIframeTopDocument = forwardFrame.contentDocument
                    if (baseData.loginPage) {                                
                        return true
                    }  else {
                        let mainiframe = forwardFrame.contentDocument.querySelector('#mainiframe')
                        if (mainiframe) {
                            baseData.mainIframe = mainiframe
                            baseData.mainIframeDocument = baseData.mainIframe.contentDocument
                            return true
                        }
                    }                 
                } else {
                    baseData.mainIframeTopDocument = document
                    if (baseData.loginPage) {    
                        return true
                    }  else {
                        let mainiframe = document.querySelector('#mainiframe')                          
                        if (mainiframe) {
                            baseData.mainIframe = mainiframe
                            baseData.mainIframeDocument = baseData.mainIframe.contentDocument
                            return true
                        }
                    }             
                }
                return false
            }).then(() => {
                resolve()
            })

        }, 600);
    }) 
}

// 应用注册页面特殊处理
setTimeout(() => {
    try {        
        let funcDiv = baseData.mainIframeDocument.querySelector('.header_in_icon');
        if (funcDiv && funcDiv.innerText.includes('应用注册')) {
          baseData.mainIframeDocument.querySelector('.nc-workbench-ownpage-right').contentEditable = 'true';
        }
    } catch (error) {
        
    }
  }, 3000);

  // 轮询mainIframe
// function PollingMainIframe () {
//   let mainiframe = baseData.mainIframeDocument.querySelector('#mainiframe');
//   if (mainiframe) {
//     GlobalVal.iframe = mainiframe;      
//     return true
//   } else {
//     console.warn('没找到iframe',location.href)
//   }
//   return false
// }